
from CvPythonExtensions import *
import CvUtil

import CvEventManager
import sys
import PyHelpers
import CvMagicUtils
import sdToolKit

gc = CyGlobalContext()
sd = sdToolKit	

###################################################
class CvMagicEventManager:

	def __init__(self, eventManager):
		# initialize base class		
		eventManager.addEventHandler("BeginPlayerTurn", self.onBeginPlayerTurn)
		
	def onInit(self, argsList):
		'Called when Civ starts up'
		CvUtil.pyPrint( 'OnInit' )
		sd.sdModSave("TemporarySaveForSpellTargeting", 0) # Used to start loop which waits for the player to select a unit in the popup		

	def onWindowActivation(self, argsList):
		'Called when the game window activates or deactivates'
		bActive = argsList[0]
		sd.sdModSave("TemporarySaveForSpellTargeting", 0) # Used to start loop which waits for the player to select a unit in the popup		
		
	def onBeginPlayerTurn(self, argsList):
		'Called at the beginning of a players turn'
		iGameTurn, iPlayer = argsList
		pRand = gc.getGame().getSorenRand()
		
		pUnitList = PyHelpers.PyPlayer(iPlayer).getUnitList()
		
		for pUnit in pUnitList:
		
			if pUnit.isHasPromotion(gc.getInfoTypeForString("PROMOTION_IMOBALIZED") ):
				if pRand.get(2, "remove imobalized") == 1:
					pUnit.setHasPromotion(gc.getInfoTypeForString("PROMOTION_IMOBALIZED"), false)
			
			if pUnit.isHasPromotion(gc.getInfoTypeForString("PROMOTION_NO_FIRST_STRIKES") ):
				if pRand.get(2, "remove no first strikes") == 1:
					pUnit.setHasPromotion(gc.getInfoTypeForString("PROMOTION_NO_FIRST_STRIKES"), false)
					
			if pUnit.isHasPromotion(gc.getInfoTypeForString("PROMOTION_CONFLAGRATION_FIRE") ):
				if pRand.get(2, "remove on fire") == 1:
					pUnit.setHasPromotion(gc.getInfoTypeForString("PROMOTION_CONFLAGRATION_FIRE"), false)
				else:
					pUnit.changeDamage(pRand.get(10, "fire damage"), -1)
			
			if pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_FIREWALL"):
				pUnit.kill(0, -1)
		
